package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.WaterMeterrechargerecord;
import com.ruoyi.system.domain.WaterUser;
import com.ruoyi.system.mapper.WaterMeterrechargerecordMapper;
import com.ruoyi.system.mapper.WaterUserMapper;
import com.ruoyi.system.service.IWaterMeterrechargerecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 充值记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-16
 */
@Service
public class WaterMeterrechargerecordServiceImpl implements IWaterMeterrechargerecordService
{
    @Autowired
    private WaterMeterrechargerecordMapper waterMeterrechargerecordMapper;

    @Autowired
    private WaterUserMapper waterUserMapper;

    /**
     * 查询充值记录
     *
     * @param id 充值记录主键
     * @return 充值记录
     */
    @Override
    public WaterMeterrechargerecord selectWaterMeterrechargerecordById(Long id)
    {
        return waterMeterrechargerecordMapper.selectWaterMeterrechargerecordById(id);
    }

    /**
     * 查询充值记录列表
     *
     * @param waterMeterrechargerecord 充值记录
     * @return 充值记录
     */
    @Override
    public List<WaterMeterrechargerecord> selectWaterMeterrechargerecordList(WaterMeterrechargerecord waterMeterrechargerecord)
    {
        return waterMeterrechargerecordMapper.selectWaterMeterrechargerecordList(waterMeterrechargerecord);
    }

    /**
     * 新增充值记录
     *
     * @param waterMeterrechargerecord 充值记录
     * @return 结果
     */
    @Override
    public int insertWaterMeterrechargerecord(WaterMeterrechargerecord waterMeterrechargerecord)
    {
        WaterUser waterUser = new WaterUser();
        waterUser.setUserId(waterMeterrechargerecord.getUserId());
        waterUser = waterUserMapper.selectWaterUserList(waterUser).get(0);
        BigDecimal a = waterUser.getRemainingsum();
        waterUser.setRemainingsum(a.add(waterMeterrechargerecord.getMoney()));
        waterUserMapper.updateWaterUser(waterUser);
        waterMeterrechargerecord.setChargeDate(DateUtils.getNowDate());
        waterMeterrechargerecord.setCreateTime(DateUtils.getNowDate());
        return waterMeterrechargerecordMapper.insertWaterMeterrechargerecord(waterMeterrechargerecord);
    }

    /**
     * 修改充值记录
     *
     * @param waterMeterrechargerecord 充值记录
     * @return 结果
     */
    @Override
    public int updateWaterMeterrechargerecord(WaterMeterrechargerecord waterMeterrechargerecord)
    {
        return waterMeterrechargerecordMapper.updateWaterMeterrechargerecord(waterMeterrechargerecord);
    }

    /**
     * 批量删除充值记录
     *
     * @param ids 需要删除的充值记录主键
     * @return 结果
     */
    @Override
    public int deleteWaterMeterrechargerecordByIds(Long[] ids)
    {
        return waterMeterrechargerecordMapper.deleteWaterMeterrechargerecordByIds(ids);
    }

    /**
     * 删除充值记录信息
     *
     * @param id 充值记录主键
     * @return 结果
     */
    @Override
    public int deleteWaterMeterrechargerecordById(Long id)
    {
        return waterMeterrechargerecordMapper.deleteWaterMeterrechargerecordById(id);
    }
}
